{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "code_folding": [
     0,
     21,
     40
    ]
   },
   "outputs": [],
   "source": [
    "def dummy2dense(fname: str):\n",
    "    dim = int(fname.split(\"_\")[1][0:-1])\n",
    "\n",
    "    data = []\n",
    "    with open(fname) as fid:\n",
    "        for line in fid:\n",
    "            feat = [\"0\"] * dim\n",
    "            tmplist = line.strip().split()\n",
    "            label = tmplist.pop(0)\n",
    "\n",
    "            for i in tmplist:\n",
    "                feat[int(i)] = \"1\"\n",
    "\n",
    "            data.append(\"{label} {feat}\".format(label=label, feat=\" \".join(feat)))\n",
    "\n",
    "    opfname = fname.split(\".\")[0] + \".dense\"\n",
    "    fid = open(opfname, \"w\")\n",
    "    fid.write(\"\\n\".join(data))\n",
    "    fid.close()\n",
    "\n",
    "\n",
    "def dummy2libsvm(fname: str):\n",
    "    data = []\n",
    "    with open(fname) as fid:\n",
    "        for line in fid:\n",
    "            feat = []\n",
    "            tmplist = line.strip().split()\n",
    "            label = tmplist.pop(0)\n",
    "\n",
    "            for i in tmplist:\n",
    "                feat.append(str(int(i)+1) + \":1\")\n",
    "\n",
    "            data.append(\"{label} {feat}\".format(label=label, feat=\" \".join(feat)))\n",
    "\n",
    "    opfname = fname.split(\".\")[0] + \".libsvm\"\n",
    "    fid = open(opfname, \"w\")\n",
    "    fid.write(\"\\n\".join(data))\n",
    "    fid.close()\n",
    "\n",
    "\n",
    "def libsvm2dummy(fname: str):\n",
    "    data = []\n",
    "    with open(fname) as fid:\n",
    "        for line in fid:\n",
    "            feat = []\n",
    "            tmplist = line.strip().split()\n",
    "            label = tmplist.pop(0)\n",
    "\n",
    "            for item in tmplist:                \n",
    "                feat.append(str(int(item.split(\":\")[0])-1))\n",
    "\n",
    "            data.append(\"{label} {feat}\".format(label=label, feat=\" \".join(feat)))\n",
    "\n",
    "    opfname = fname.split(\".\")[0] + \".dummy\"\n",
    "    fid = open(opfname, \"w\")\n",
    "    fid.write(\"\\n\".join(data))\n",
    "    fid.close()\n",
    "\n",
    "\n",
    "def libsvm2dense(fname: str):\n",
    "    dim = int(fname.split(\"_\")[1][0:-1])\n",
    "    \n",
    "    data = []\n",
    "    with open(fname) as fid:\n",
    "        for line in fid:\n",
    "            feat = [\"0.0\"] * dim\n",
    "            tmplist = line.strip().split()\n",
    "            label = tmplist.pop(0)\n",
    "\n",
    "            for item in tmplist:\n",
    "                (idx, val) = item.split(\":\")\n",
    "                idx = int(idx)-1\n",
    "                feat[idx] = val\n",
    "\n",
    "            data.append(\"{label} {feat}\".format(label=label, feat=\" \".join(feat)))\n",
    "\n",
    "    opfname = fname.split(\".\")[0] + \".dense\"\n",
    "    fid = open(opfname, \"w\")\n",
    "    fid.write(\"\\n\".join(data))\n",
    "    fid.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = r\"census\\census_148d_train.dummy\"\n",
    "dummy2libsvm(filename)\n",
    "dummy2dense(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = r\"abalone\\abalone_8d_train.libsvm\"\n",
    "libsvm2dense(filename)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": true,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
